package com.yixun.qfbao.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.api.service.AgentRuleService;
import com.yixun.qfbao.apidto.GetAgentRuleResDto;
import com.yixun.qfbao.apidto.ModelResDto;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.ModelService;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2021-2-19
 *
 * desc:
 *     
 */
@Service("agentRuleService")
public class AgentRuleServiceImpl implements AgentRuleService{
	
	@Autowired
	CompanyService companyService;

	@Autowired
	ModelService modelService;
	
	/**
	 * 获取分销的决策规则
	 */
	@Override
	public GetAgentRuleResDto getAgentRule(Long companyId, String enableWeappMsg) {
		
		Company entity = new Company();
		entity.setId(companyId);
		entity = companyService.selectBy(entity);
		GetAgentRuleResDto getAgentRuleResDto = new GetAgentRuleResDto();
		getAgentRuleResDto.setResult(ResultEnum.SUCCESS.getNo());
		if(entity == null) {
			getAgentRuleResDto.setResult(ResultEnum.FAIL.getNo());
			getAgentRuleResDto.setResultMsg("未找到公司");
			return getAgentRuleResDto;
		}
		if(!"1".equals(enableWeappMsg)) {
			getAgentRuleResDto.setCompanyName(entity.getCompanyName());
			return getAgentRuleResDto;
		}
		
		// 获取经纪人注册审核是否开启
		getAgentRuleResDto.setPartnerCheckFlag("1".equals(entity.getEnablePartnerCheck()));
		
		// 获取所有模板
		Model model = new Model();
		model.setType(ModelTypeEnum2.PARTNER_CHECK.getNo());
		model.setCompanyId(companyId);
//		model.setCreateUserId(userId);
		List<Model> modelList = modelService.selectListByDto(model);
		
		if(CollectionUtils.isEmpty(modelList)) {
			return getAgentRuleResDto;
		}
		
		List<ModelResDto> modelResDtoList = ModelResDto.listConvertModelToModelResDto(modelList);
		getAgentRuleResDto.setModelList(modelResDtoList);
		return getAgentRuleResDto;
	}

}
